package com.wtwd.device.service.impl;


import com.wtwd.device.constant.ClientTypeEnum;
import com.wtwd.device.constant.PushConstant;
import com.wtwd.device.model.dto.PushMsgDTO;
import com.wtwd.device.model.vo.ResultVO;
import com.wtwd.device.push.AliyunMobilePushUtil;
import com.wtwd.device.push.PushEntity;
import com.wtwd.device.service.PushService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @author zdl
 * @date 2021/11/22 17:41
 */
@Service
public class PushServiceImpl implements PushService {

    @Value("${push.appkey-ios}")
    private Long iosAppKey;

    @Value("${push.appkey-android}")
    private Long androidAppkey;

    @Value("${push.env}")
    private String env;


    @Override
    public ResultVO pushMsg(PushMsgDTO pushMsgDTO) {
        PushEntity pushEntity = new PushEntity();
        if (pushMsgDTO.getClientType().equals(ClientTypeEnum.ANDROID.getCode())) {//安卓
            pushEntity.setAppKey(androidAppkey);
            pushEntity.setDeviceType(ClientTypeEnum.ANDROID.getValue());
        } else if (pushMsgDTO.getClientType().equals(ClientTypeEnum.IOS.getCode())) {//ios
            pushEntity.setAppKey(iosAppKey);
            pushEntity.setDeviceType(ClientTypeEnum.IOS.getValue());
            pushEntity.setEnv(env);
        }
        pushEntity.setPushType(PushConstant.PUSH_TYPE_NOTICE);
        pushEntity.setTarget(PushConstant.DEVICE_PUSH_TARGET_ACCOUNT);
        pushEntity.setTargetValue(pushMsgDTO.getToUser());
        pushEntity.setTitle(pushMsgDTO.getMsgTitle());
        pushEntity.setBody(pushMsgDTO.getMsgContent());
        pushEntity.setExtraParams(pushMsgDTO.getExtraParams());
        return AliyunMobilePushUtil.pushApp(pushEntity);
    }


    @Override
    public ResultVO pushNoticeUser(PushMsgDTO pushMsgDTO) {
        PushEntity pushEntity = new PushEntity();
        if (pushMsgDTO.getClientType().equals(ClientTypeEnum.ANDROID.getCode())) {//安卓
            pushEntity.setAppKey(androidAppkey);
            pushEntity.setDeviceType(ClientTypeEnum.ANDROID.getValue());
        } else if (pushMsgDTO.getClientType().equals(ClientTypeEnum.IOS.getCode())) {//ios
            pushEntity.setAppKey(iosAppKey);
            pushEntity.setDeviceType(ClientTypeEnum.IOS.getValue());
        }
        pushEntity.setPushType(PushConstant.PUSH_TYPE_NOTICE);
        pushEntity.setTarget(PushConstant.DEVICE_PUSH_TARGET_ACCOUNT);
        pushEntity.setTargetValue(pushMsgDTO.getToUser());
        pushEntity.setTitle(pushMsgDTO.getMsgTitle());
        pushEntity.setBody(pushMsgDTO.getMsgContent());
        return AliyunMobilePushUtil.pushApp(pushEntity);
    }

    @Override
    public ResultVO pushMessageUserDevice(PushMsgDTO pushMsgDTO) {
        PushEntity pushEntity = new PushEntity();
        if (pushMsgDTO.getClientType().equals(ClientTypeEnum.ANDROID.getCode())) {//安卓
            pushEntity.setAppKey(androidAppkey);
            pushEntity.setDeviceType(ClientTypeEnum.ANDROID.getValue());
        } else if (pushMsgDTO.getClientType().equals(ClientTypeEnum.IOS.getCode())) {//ios
            pushEntity.setAppKey(iosAppKey);
            pushEntity.setDeviceType(ClientTypeEnum.IOS.getValue());
        }
        pushEntity.setPushType(PushConstant.PUSH_TYPE_MESSAGE);
        pushEntity.setTarget(PushConstant.DEVICE_PUSH_TARGET_DEVICE);
        pushEntity.setTargetValue(pushMsgDTO.getToUser());
        pushEntity.setTitle(pushMsgDTO.getMsgTitle());
        pushEntity.setBody(pushMsgDTO.getMsgContent());
        return AliyunMobilePushUtil.pushApp(pushEntity);
    }

    @Override
    public ResultVO pushNoticeUserDevice(PushMsgDTO pushMsgDTO) {
        PushEntity pushEntity = new PushEntity();
        if (pushMsgDTO.getClientType().equals(ClientTypeEnum.ANDROID.getCode())) {//安卓
            pushEntity.setAppKey(androidAppkey);
            pushEntity.setDeviceType(ClientTypeEnum.ANDROID.getValue());
        } else if (pushMsgDTO.getClientType().equals(ClientTypeEnum.IOS.getCode())) {//ios
            pushEntity.setAppKey(iosAppKey);
            pushEntity.setDeviceType(ClientTypeEnum.IOS.getValue());
        }
        pushEntity.setPushType(PushConstant.PUSH_TYPE_NOTICE);
        pushEntity.setTarget(PushConstant.DEVICE_PUSH_TARGET_DEVICE);
        pushEntity.setTargetValue(pushMsgDTO.getToUser());
        pushEntity.setTitle(pushMsgDTO.getMsgTitle());
        pushEntity.setBody(pushMsgDTO.getMsgContent());
        return AliyunMobilePushUtil.pushApp(pushEntity);
    }


}
